package com.tg.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tg.app.R;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;
import com.tg.app.util.LogUtils;
import com.tg.app.view.lazy.LazyViewPagerAdapter;

import java.util.List;

/**
 * Describe:
 * Created by Rance Rang on 2019-12-27.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class MultiPlayPagerAdapter extends LazyViewPagerAdapter {

    private static final String TAG = "MultiPlayerPagerAdapter";
    // 条目总数
    private int mItemCount;
    // 每页显示条目数量
    private int mPageSize = 4;
    // 每页列数
    private int mNumColumns = 2;
    // 总页数
    private int mPageCount;

    private Context  context ;
    private List<DeviceItem> itemList;
    private View.OnTouchListener onTouchListener;

    public void setOnTouchListener(View.OnTouchListener onTouchListener) {
        this.onTouchListener = onTouchListener;
    }
    public  MultiPlayPagerAdapter(Context context,  List<DeviceItem> items){
        this.context = context;
        this.itemList = items;
    }

    @Override
    protected View getItem(ViewGroup container, int position) {

        TGLog.d(TAG, "SplitScreenPlayer getItem id:"+position);
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.widget_recycler_view_pager, null);
        RecyclerView rvPager = view.findViewById(R.id.rv_rvg_pager);
        // 设置布局为GridView布局
        rvPager.setLayoutManager(new GridLayoutManager(container.getContext(), mNumColumns));

        // 设置每个RecyclerView的适配器
        setGVAdapterOrRefresh(rvPager, position);

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (onTouchListener!=null){
                    onTouch(v,event);
                }
                return false;
            }
        });
        // 显示该页面
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        RecyclerView rvPager = container.findViewById(R.id.rv_rvg_pager);
        TGLog.d(TAG, "SplitScreenPlayer destroyItem id:"+position);
//        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        if (itemList != null) {
            // 计算ViewPager的页数
            mPageCount = itemList.size() % mPageSize == 0 ? itemList.size() / mPageSize : itemList.size() / mPageSize + 1;
        }
        return mPageCount;
    }
    private void setGVAdapterOrRefresh(RecyclerView rv, int position) {
        RecyclerView.Adapter adapter = rv.getAdapter();
        // 如果已经设置过适配器，直接刷新即可
        if (null != adapter) {
            adapter.notifyDataSetChanged();
        } else {

            SinglePlayAdapter singlePlayAdapter = new SinglePlayAdapter(context,itemList);
            singlePlayAdapter.setIndex(position);
            singlePlayAdapter.setPageSize(mPageSize);
            singlePlayAdapter.setPageCount(mPageCount);
            rv.setAdapter(singlePlayAdapter);
        }
    }
}
